<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <!-- Bootstrap CSS -->
    <link href="__STATIC__/libs/agent/bootstrap.min.css" rel="stylesheet">
    <!-- Chart.js -->
    <script src="__STATIC__/libs/agent/chart.js"></script>
    <!-- Font Awesome -->
    <link href="__STATIC__/libs/agent/all.min.css" rel="stylesheet">
    <!-- Flatpickr -->
    <link rel="stylesheet" href="__STATIC__/libs/agent/flatpickr.min.css">
    <script src="__STATIC__/libs/agent/flatpickr.js"></script>
    <script src="__STATIC__/libs/agent/zh.js"></script>
    <script src="__STATIC__/libs/jquery/jquery.min.js"></script>
    <style>
        :root {
            --primary-color: #FF8C00;
            --secondary-color: #FFA500;
            --accent-color: #FFD700;
            --dark-bg: #F0F8FF;
            --card-bg: #FFFFFF;
            --gradient-start: #FF8C00;
            --gradient-end: #FFA500;
            --success-color: #FFB74D;
            --danger-color: #FF7043;
            --text-primary: #333333;
            --text-secondary: #666666;
            --border-color: #E0E0E0;
        }

        body {
            background-color: var(--dark-bg);
            color: var(--text-primary);
            font-family: 'Arial', sans-serif;
            min-height: 100vh;
        }

        .dashboard-container {
            padding: 15px;
            max-width: 1800px;
            margin: 0 auto;
        }

        .dashboard-header {
            background: linear-gradient(135deg, #FF8C00, #FFA500);
            padding: 15px;
            border-radius: 15px;
            margin-bottom: 15px;
            box-shadow: 0 4px 15px rgba(255, 140, 0, 0.3);
        }

        .dashboard-header h1 {
            font-size: 24px;
            margin-bottom: 10px;
        }

        .card {
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            border-radius: 15px;
            margin-bottom: 15px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .card:hover::before {
            opacity: 1;
        }

        .card-header {
            background-color: var(--card-bg);
            border-bottom: 1px solid var(--border-color);
            border-radius: 15px 15px 0 0 !important;
            padding: 12px 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-header h2 {
            color: var(--text-primary);
        }

        .date-range-picker {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .date-range-picker input {
            background: var(--card-bg);
            border: 1px solid var(--border-color);
            color: var(--text-primary);
            padding: 6px 10px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 12px;
        }

        .date-range-picker input:focus {
            outline: none;
            box-shadow: 0 0 0 2px var(--primary-color);
        }

        .stat-card {
            background: linear-gradient(135deg, #FF8C00, #FFA500);
            color: white;
            padding: 15px;
            border-radius: 12px;
            margin-bottom: 15px;
            position: relative;
            overflow: hidden;
            transition: transform 0.3s ease;
            box-shadow: 0 4px 15px rgba(255, 140, 0, 0.2);
        }

        .stat-card:hover {
            transform: translateY(-3px);
        }

        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(45deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0));
            z-index: 1;
        }

        .stat-card h3 {
            font-size: 14px;
            margin-bottom: 8px;
            position: relative;
            z-index: 2;
        }

        .stat-card p {
            font-size: 24px;
            margin: 0;
            font-weight: bold;
            position: relative;
            z-index: 2;
        }

        .stat-card .icon {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 30px;
            opacity: 0.15;
            z-index: 1;
            color: #000000;
        }

        .chart-container {
            position: relative;
            height: 250px;
            margin: 10px 0;
            padding: 10px;
            background: var(--card-bg);
            border-radius: 12px;
            border: 1px solid var(--border-color);
        }

        .time-selector {
            display: flex;
            gap: 8px;
        }

        .time-selector button {
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            color: var(--text-primary);
            padding: 6px 12px;
            border-radius: 20px;
            transition: all 0.3s ease;
            font-size: 12px;
        }

        .time-selector button:hover {
            background-color: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        .time-selector button.active {
            background-color: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        .trend-indicator {
            display: inline-flex;
            align-items: center;
            margin-left: 8px;
            font-size: 12px;
            color: #000000;
            opacity: 0.8;
        }

        .trend-up {
            color: #000000;
        }

        .trend-down {
            color: #000000;
        }

        .quick-stats {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
            margin-bottom: 15px;
        }

        .quick-stat-card {
            background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
            padding: 12px;
            border-radius: 12px;
            text-align: center;
            transition: transform 0.3s ease;
        }

        .quick-stat-card:hover {
            transform: translateY(-3px);
        }

        .quick-stat-card i {
            font-size: 18px;
            margin-bottom: 5px;
            color: #000000;
            opacity: 0.8;
        }

        .quick-stat-card h3 {
            color: #000000;
            opacity: 0.8;
        }

        .quick-stat-card p {
            color: #000000;
            font-weight: bold;
        }

        /* 排行榜样式优化 */
        .ranking-section {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
            margin-top: 15px;
        }

        .ranking-card {
            background: var(--card-bg);
            border: 1px solid var(--border-color);
            border-radius: 12px;
            padding: 12px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        }

        .ranking-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }

        .ranking-title {
            display: flex;
            align-items: center;
            gap: 8px;
            color: var(--text-primary);
            font-size: 14px;
            margin: 0;
        }

        .ranking-time-selector {
            display: flex;
            gap: 4px;
        }

        .ranking-time-selector button {
            background: var(--card-bg);
            border: 1px solid var(--border-color);
            color: var(--text-primary);
            padding: 3px 6px;
            border-radius: 4px;
            font-size: 11px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .ranking-time-selector button:hover {
            background: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        .ranking-time-selector button.active {
            background: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        .ranking-list {
            list-style: none;
            padding: 0;
            margin: 0;
            max-height: 200px;
            overflow-y: auto;
            scrollbar-width: thin;
            scrollbar-color: var(--primary-color) var(--card-bg);
        }

        .ranking-list::-webkit-scrollbar {
            width: 4px;
        }

        .ranking-list::-webkit-scrollbar-track {
            background: var(--card-bg);
        }

        .ranking-list::-webkit-scrollbar-thumb {
            background-color: var(--primary-color);
            border-radius: 2px;
        }

        .ranking-item {
            display: flex;
            align-items: center;
            padding: 8px 0;
            border-bottom: 1px solid var(--border-color);
        }

        .ranking-item:last-child {
            border-bottom: none;
        }

        .rank-number {
            width: 20px;
            height: 20px;
            background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 11px;
            margin-right: 8px;
        }

        .rank-info {
            flex: 1;
        }

        .rank-name {
            font-size: 12px;
            margin-bottom: 2px;
        }

        .rank-value {
            font-size: 14px;
            font-weight: bold;
            color: var(--primary-color);
        }

        .rank-trend {
            font-size: 11px;
            color: #4CAF50;
            margin-left: 6px;
        }

        /* 调整网格布局 */
        @media (min-width: 1200px) {
            .quick-stats {
                grid-template-columns: repeat(4, 1fr);
            }

            .ranking-section {
                grid-template-columns: repeat(3, 1fr);
            }
        }

        @media (max-width: 1199px) {
            .quick-stats {
                grid-template-columns: repeat(2, 1fr);
            }

            .ranking-section {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        @media (max-width: 767px) {
            .quick-stats {
                grid-template-columns: 1fr;
            }

            .ranking-section {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<div class="dashboard-container">
    <div class="dashboard-header">
        <h1 class="text-center mb-3">数据大屏</h1>
        <div class="time-selector justify-content-center" id="data-type">
            <button class="active" data-id="1">今日</button>
            <button data-id="2">本周</button>
            <button data-id="3">本月</button>
            <button data-id="4">本年</button>
        </div>
    </div>

    <!-- 数据概括 -->
    <div class="quick-stats">
        <div class="quick-stat-card">
            <i class="fas fa-users"></i>
            <h3>新增推客</h3>
            <p id="tk-add">0</p>
        </div>
        <div class="quick-stat-card">
            <i class="fas fa-shopping-cart"></i>
            <h3>订单数量</h3>
            <p id="orders">0</p>
        </div>
        <div class="quick-stat-card">
            <i class="fas fa-yen-sign"></i>
            <h3>总销售额</h3>
            <p>¥<span id="total">0.00</span></p>
        </div>
        <div class="quick-stat-card">
            <i class="fas fa-hand-holding-usd"></i>
            <h3>总佣金</h3>
            <p>¥<span id="commission">0.00</span></p>
        </div>
    </div>

    <!-- 用户量统计 -->
    <div class="card">
        <div class="card-header">
            <h2><i class="fas fa-users me-2"></i>推客用户分析</h2>
            <div class="date-range-picker">
                <input id="user-date" type="text" class="date-range" placeholder="选择日期范围">
            </div>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <div class="stat-card">
                        <i class="fas fa-users icon"></i>
                        <h3>总用户数</h3>
                        <p id="users">0</p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <i class="fas fa-user-friends icon"></i>
                        <h3>总推客数</h3>
                        <p id="tk-users">0</p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <i class="fas fa-user-plus icon"></i>
                        <h3>新增推客</h3>
                        <p id="user-add">0</p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <i class="fas fa-user-check icon"></i>
                        <h3>活跃推客</h3>
                        <p id="user-active">0</p>
                    </div>
                </div>
            </div>
            <div class="layui-row layui-col-space10">
                <div class="layui-col-lg12 layui-col-md12">
                    <div id="user-chart" style="width: 100%; height: 400px; margin-top: 30px;"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- 订单统计 -->
    <div class="card">
        <div class="card-header">
            <h2><i class="fas fa-shopping-bag me-2"></i>订单分析</h2>
            <div class="date-range-picker">
                <input id="order-date" type="text" class="date-range" placeholder="选择日期范围">
            </div>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="stat-card">
                        <i class="fas fa-shopping-cart icon"></i>
                        <h3>订单总量</h3>
                        <p id="order-total">0</p>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="stat-card">
                        <i class="fas fa-yen-sign icon"></i>
                        <h3>订单金额</h3>
                        <p>¥<span id="sales">0.00</span></p>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="stat-card">
                        <i class="fas fa-hand-holding-usd icon"></i>
                        <h3>佣金</h3>
                        <p>¥<span id="commissions">0.00</span></p>
                    </div>
                </div>
            </div>
            <div class="layui-row layui-col-space10">
                <div class="layui-col-lg12 layui-col-md12">
                    <div id="order-chart" style="width: 100%; height: 400px; margin-top: 30px;"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- 排行榜 -->
    <div class="ranking-section">
        <!-- 产品佣金排行 -->
        <div class="ranking-card">
            <div class="ranking-header">
                <h3 class="ranking-title"><i class="fas fa-crown"></i>产品佣金排行</h3>
                <div id="goods-type" class="ranking-time-selector">
                    <button class="active" data-id="1">今日</button>
                    <button data-id="2">本周</button>
                    <button data-id="3">本月</button>
                    <button data-id="4">今年</button>
                </div>
            </div>
            <div class="ranking-date-picker">
                <input id="goods-time" type="text" class="date-range" placeholder="选择日期范围">
            </div>
            <ul class="ranking-list goods">
            </ul>
        </div>

        <!-- 推客收益排行 -->
        <div class="ranking-card">
            <div class="ranking-header">
                <h3 class="ranking-title"><i class="fas fa-medal"></i>推客收益排行</h3>
                <div id="users-type" class="ranking-time-selector">
                    <button class="active" data-id="1">今日</button>
                    <button data-id="2">本周</button>
                    <button data-id="3">本月</button>
                    <button data-id="4">今年</button>
                </div>
            </div>
            <div class="ranking-date-picker">
                <input id="users-time" type="text" class="date-range" placeholder="选择日期范围">
            </div>
            <ul class="ranking-list">
                <li class="ranking-item users">
                    <div class="rank-number">1</div>
                    <div class="rank-info">
                        <div class="rank-name">张三</div>
                        <div class="rank-value">¥45,678</div>
                    </div>
                </li>
            </ul>
        </div>

        <!-- 推客邀请排行 -->
        <div class="ranking-card">
            <div class="ranking-header">
                <h3 class="ranking-title"><i class="fas fa-trophy"></i>推客邀请排行</h3>
                <div id="invite-type" class="ranking-time-selector">
                    <button class="active" data-id="1">今日</button>
                    <button data-id="2">本周</button>
                    <button data-id="3">本月</button>
                    <button data-id="4">今年</button>
                </div>
            </div>
            <div class="ranking-date-picker">
                <input id="invite-time" type="text" class="date-range" placeholder="选择日期范围">
            </div>
            <ul class="ranking-list invite">
            </ul>
        </div>
    </div>
</div>

<!-- Bootstrap JS -->
<script src="__STATIC__/libs/agent/bootstrap.bundle.min.js"></script>
<script src="__STATIC__/libs/echarts/echarts.min.js"></script>
<script>
    // 用户量图表
    var userChart = echarts.init(document.getElementById('user-chart'));
    // 订单图表
    var orderChart = echarts.init(document.getElementById('order-chart'));
    const charts = {
        user: userChart,
        order: orderChart,
    };

    window.addEventListener('resize', function () {
        Object.values(charts).forEach(chart => {
            if (chart) chart.resize();
        });
    });

    // 初始化日期选择器
    document.querySelectorAll('.date-range').forEach(input => {
        flatpickr(input, {
            mode: "range",
            dateFormat: "Y-m-d",
            locale: "zh",
            theme: "dark",
            onClose: function (selectedDates, dateStr, instance) {
                if (instance.input.id === 'user-date') {
                    updateChart('user', userChart, dateStr);
                } else if (instance.input.id === 'order-date') {
                    updateChart('order', orderChart, dateStr);
                } else if (instance.input.id === 'goods-time') {
                    get_ranking_data('goods', '', dateStr)
                } else if (instance.input.id === 'users-time') {
                    get_ranking_data('users', '', dateStr)
                } else if (instance.input.id === 'invite-time') {
                    get_ranking_data('invite', '', dateStr)
                }
            }
        });
    });

    updateChart('user', userChart);
    updateChart('order', orderChart);

    // 更新图表的函数
    function updateChart(type, chart, date) {
        fetchData(type, date).then(data => {
            let option = getOption(type, data);
            chart.setOption(option, true);
        });
    }

    // 使用 fetch 获取数据的函数
    function fetchData(type, date = '') {
        let url = '{:url("admin_data/line")}?type=' + type + '&date=' + date;
        return fetch(url)
            .then(response => response.json())
            .then(data => {
                return data;
            })
            .catch(error => {
                return {total: [0, 0, 0, 0, 0, 0, 0]}; // 返回默认空数据
            });
    }

    // 获取图表配置的函数
    function getOption(type, data) {
        var xAxisData = data.xAxis;
        var series = [];
        var legend = [];
        var title = '';

        switch (type) {
            case 'user':
                if (data.count) {
                    let count = data.count;
                    $('#user-add').text(count.user_add);
                    $('#user-active').text(count.user_active);
                }

                series = [
                    {name: '新增推客', type: 'line', data: data.data.total, itemStyle: {color: '#FFA500'}},
                    // {name: '活跃推客', type: 'line', data: data.data.active},
                ];
                legend = ['新增推客'];
                break;
            default:
                if (data.count) {
                    let count = data.count;
                    $('#order-total').text(count.total);
                    $('#sales').text(count.sales);
                    $('#commissions').text(count.commissions);
                }

                series = [
                    {name: '订单总量', type: 'bar', data: data.data.total, itemStyle: {color: '#FFA500'}},
                    // {name: '订单金额', type: 'line', data: data.data.sales},
                    // {name: '佣金', type: 'line', data: data.data.commissions},
                ];
                legend = ['订单总量'];

        }
        return {
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            toolbox: {
                feature: {
                    magicType: {
                        type: ['line', 'bar'],
                        title: {
                            line: '折线图切换',
                            bar: '柱形图切换',
                        },
                    },
                    saveAsImage: {
                        show: true,
                        title: '保存为图片',
                        type: 'png',
                        lang: ['点击保存']
                    }
                }
            },
            legend: {
                data: legend,
                height: 'auto'
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: xAxisData,
                boundaryGap: [0, 0.01]
            },
            yAxis: {
                type: 'value',
            },
            series: series
        };
    }

    // 时间选择器功能
    document.querySelectorAll('.time-selector button, .ranking-time-selector button').forEach(button => {
        button.addEventListener('click', function () {
            // 找到当前按钮所在的按钮组
            const buttonGroup = this.closest('.time-selector, .ranking-time-selector');
            // 移除同组中所有按钮的active类
            buttonGroup.querySelectorAll('button').forEach(btn => {
                btn.classList.remove('active');
            });
            // 为当前按钮添加active类
            this.classList.add('active');
        });
    });

    get_data('1');
    get_ranking_data('goods', '1')
    get_ranking_data('users', '1')
    get_ranking_data('invite', '1')

    $('#data-type button').click(function () {
        let data_id = $(this).attr('data-id');
        get_data(data_id);
    })

    function get_data(type) {
        $.get('{:url("admin_data/index")}', {
            type: type
        }, function (data) {
            if (data.code === 0) {
                $('#users').text(data.data.users);
                $('#tk-add').text(data.data.tk_add);
                $('#tk-users').text(data.data.tk_users);
                $('#orders').text(data.data.orders);
                $('#total').text(data.data.total);
                $('#commission').text(data.data.commission);
            }
        })
    }

    $('#goods-type button').click(function () {
        let data_id = $(this).attr('data-id');
        get_ranking_data('goods', data_id);
    })
    $('#users-type button').click(function () {
        let data_id = $(this).attr('data-id');
        get_ranking_data('users', data_id);
    })
    $('#invite-type button').click(function () {
        let data_id = $(this).attr('data-id');
        get_ranking_data('invite', data_id);
    })

    function get_ranking_data(type, date_type, date) {
        $.get('{:url("admin_data/ranking_data")}', {
            type: type,
            date_type: date_type,
            date: date,
        }, function (data) {
            if (type == 'users') {
                $('.users').html('')
            } else if (type == 'invite') {
                $('.invite').html('')
            } else {
                $('.goods').html('')
            }
            if (data.code === 0) {
                data.data.forEach(function (item, index) {
                    let html = `<li class="ranking-item">
                                        <div class="rank-number">${index + 1}</div>
                                        <div class="rank-info">
                                            <div class="rank-name">${item.title}</div>
                                            <div class="rank-value">${item.total}</div>
                                        </div>
                                    </li>`
                    if (type == 'users') {
                        $('.users').append(html);
                    } else if (type == 'invite') {
                        $('.invite').append(html);
                    } else {
                        $('.goods').append(html);
                    }
                })
            }
        })
    }

</script>
</body>
</html>
